﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;   
using System.IO;
public partial class Main_Reports_Inventory_ValuationReport : System.Web.UI.Page
{
    Datautility DU = new Datautility(); 
    DataTable Dt = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    private void getData()
    {
        if (txtfromdate.Text != "" && txttodate.Text != "")
        {
            lblperiod.Text = "Period :" + txtfromdate.Text + " to " + txttodate.Text;
            SqlParameter[] PurchaseParam = { 
                                            new SqlParameter("@fromdate",Convert.ToDateTime(txtfromdate.Text).ToString(string.Format("yyyy/MM/dd"))),
                                            new SqlParameter("@todate",Convert.ToDateTime(txttodate.Text).ToString(string.Format("yyyy/MM/dd")))
                                        };
            DataTable Dt = DU.getdatausingprocedure("IVR_RPT_VALUATION_REPORT", PurchaseParam);
            if (Dt.Rows.Count > 0)
            {
                grdValuationReport.DataSource = Dt;
                grdValuationReport.DataBind();
                lblRecordNo.Text = grdValuationReport.Rows.Count.ToString() + " Record(s)";
                lblperiod.Text = "Period :" + txtfromdate.Text + " to " + txttodate.Text;
            }
        }
    }
    protected void btngetreport_Click(object sender, EventArgs e)
    {
        getData();
    }
    protected void btnReport_Click(object sender, EventArgs e)
    {
        grdValuationReport.Visible = true;
        getData();
        string Filter = "";
        if (ddlColumnname.SelectedValue == "1")//for Item Code
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "ItemCode like'%" + txtValue.Text + "%'");
            grdValuationReport.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "2")//for Item name
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "ItemName Like'%" + txtValue.Text + "%'");
            grdValuationReport.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "3")//For Project
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "Project Like'%" + txtValue.Text + "%'");
            grdValuationReport.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "4")//For BinNo
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "BinNo Like'%" + txtValue.Text + "%'");
            grdValuationReport.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "5")//For MPN
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "MPN Like'%" + txtValue.Text + "%'");
            grdValuationReport.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "6")//Item Group
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "ItemGroup='" + txtValue.Text + "'");
            grdValuationReport.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "7") //For the Inventory Class
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "InventoryClass='" + txtValue.Text + "'");
            grdValuationReport.AllowPaging = false;
        }
        else
        {
            getData();
        }
        DataRow[] Dr = Dt.Select(Filter);
        DataTable Tempdt = new DataTable();
        Tempdt = Dt.Clone();
        foreach (DataRow drNew in Dr)
        {
            Tempdt.ImportRow(drNew);
        }
        grdValuationReport.DataSource = Tempdt;
        grdValuationReport.DataBind();
    }   
    public override void VerifyRenderingInServerForm(Control control)
    {
        //base.VerifyRenderingInServerForm(control);
    }
    protected void ImgBtnExcel_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Valuation_Report.xls"));
            Response.ContentType = "application/ms-excel";
            grdValuationReport.ShowFooter = false;
            grdValuationReport.AllowPaging = false;
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            grdValuationReport.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.End();
        }
        catch (Exception ex)
        {
        }
    }
}